package cn.educate.servlet;

import cn.educate.utils.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取前端参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String occupation = request.getParameter("occupation"); // 教师/学生
        String gender = request.getParameter("gender");         // 男/女
        String email = request.getParameter("email");

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            // 检查用户名和邮箱是否已存在
            if (isUsernameExists(conn, username)) {
                response.getWriter().println("用户名已存在！重新注册");
                return;
            }
            if (isEmailExists(conn, email)) {
                response.getWriter().println("邮箱已注册！重新注册");
                return;
            }

            // 插入数据库
            String sql = "INSERT INTO users (name, password, occupation, gender, email) " +
                    "VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, occupation);
            pstmt.setString(4, gender);
            pstmt.setString(5, email);

            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                response.getWriter().println("注册成功！前往登录");
            } else {
                response.getWriter().println("注册失败！重新注册");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            response.getWriter().println("注册失败：数据库错误！重新注册");
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
    }

    // 检查用户名是否存在
    private boolean isUsernameExists(Connection conn, String username) throws SQLException {
        String sql = "SELECT COUNT(*) FROM users WHERE name = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            try (java.sql.ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }

    // 检查邮箱是否存在
    private boolean isEmailExists(Connection conn, String email) throws SQLException {
        String sql = "SELECT COUNT(*) FROM users WHERE email = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, email);
            try (java.sql.ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }
}